//
//  Bullet.swift
//  ShapeSurvival
//
//  Created by Jerry H on 1/9/15.
//  Copyright (c) 2015 HeylmunProgramming. All rights reserved.
//

import Foundation; import SpriteKit

class Bullet: SKSpriteNode {
    var direction: Double = 0.00
    var range: Double = 500.00
    var bulletXSpeed: CGFloat = 0.0
    var bulletYSpeed: CGFloat = 0.0
    var bulletspeed: CGFloat = 200.0
    var hitbox: CGFloat = 30
    var offSet: CGFloat = 1.0
    
    func setUp(){
        self.position.x = player.position.x + (player.xspeed * offSet);
        self.position.y = player.position.y + (player.yspeed * offSet);
        self.xScale = 0.20
        self.yScale = 0.20
        self.zPosition = 0.6
        bulletXSpeed = player.xspeed
        bulletYSpeed = player.yspeed
        
    }
    
    func inBounds() -> Bool {
        if(self.position.x > highX || self.position.x < lowX || self.position.y > highY || self.position.y < lowY){
            return false
        }
        else {
            return true
        }
    }
    
    func moveBullet(){
        var bdegrees = CGVector(dx: (bulletXSpeed * (bulletspeed / 25)), dy: (bulletYSpeed * (bulletspeed / 25)))
        let bmove = SKAction.moveBy(bdegrees, duration: 0.0)
        self.runAction(bmove)
    }
    
    func checkHits(){
        var hit = false
        for enemy: Enemy in eManager.currentEnemies {
            if( hit == false && enemy.position.x > (self.position.x - hitbox) && enemy.position.x < (self.position.x + hitbox) && enemy.position.y > (self.position.y - hitbox) && enemy.position.y < (self.position.y + hitbox) ) {
                if(enemy.canBeShot == true){
                    enemy.takeDamage();
                    let tempP = find(currentBullets, self)
                    var setP = -1
                    if(tempP? != nil){
                        setP = Int(tempP!)
                        currentBullets.removeAtIndex(setP)
                        self.removeFromParent();
                    }
                    hit = true
                }
            }
        }
    }
}
